//
//  LJDatePickerView.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LJDatePickerView.h"
#import "TimeModel.h"

@interface LJDatePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate> {
    UIPickerView *_pickerView; //初始化一个自定义的pickView
    
    NSMutableArray *yearArray;   //年
    NSMutableArray *monthArray ; //月
    NSMutableArray *dayArray;    //日
    NSMutableArray *hourArray;   //时
    NSMutableArray *minuteArray ;//分
    
    NSInteger currentYear;  //当前年
    NSInteger currentMonth; //当前月
    NSInteger currentDay;   //当前日
    NSInteger currentHour;  //当前时
    NSInteger currentMintue;//当前分
    
    NSInteger selectOneRow; //选中的行
    NSInteger selectTwoRow; //第二个选中的行
    NSInteger selectThreeRow;//第三个选中的行
    NSInteger selectFourRow; //第四个选中的行
    NSInteger selectFiveRow; //第五个选中的行
    
    NSString *hourStr; // 选择的时间(带时 、分)
    NSString *minuteStr;// 同上
}

@property(nonatomic, strong) UIView *baseView;  //设置一下背景View

@end

@implementation LJDatePickerView

- (instancetype)initWithDateMode:(LJDatePickerViewMode)mode {
    if (self = [super init]) {
        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.dateMode = mode;
        [self ConfigUI];
    }
    return self;
}

- (void)ConfigUI {
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH, ScreenW, 250 + BottomHeight)];
    self.baseView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.baseView];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 205)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [self.baseView addSubview:_pickerView];
    
    //设置取消按钮
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame  = CGRectMake(0, 205, ScreenW/2, 45);
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:HexColor(0x999999) forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:cancleBtn];

    //设置确定按钮
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(ScreenW/2, 205, ScreenW/2, 45);
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn setTitleColor:HexColor(0x999999) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:sureBtn];
    
    hourArray = [NSMutableArray arrayWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", nil];
    minuteArray = [NSMutableArray arrayWithObjects:@"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59", nil];
    yearArray  =[NSMutableArray array];
    monthArray =[NSMutableArray arrayWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];
    dayArray= [NSMutableArray array];
    [self refreshDayArrayWithYear:[TimeModel getCurrentYear] WithMonth:[TimeModel getCurrentMonth]];
    currentYear = [TimeModel getCurrentYear];
    currentMonth = [TimeModel getCurrentMonth];
    currentDay = [TimeModel getCurrentDay];
    currentHour = [TimeModel getCurrentHour];
    currentMintue = [TimeModel getCurrentMintue];
    for (int i=0; i<101; i++) {
        [yearArray addObject:[NSString stringWithFormat:@"%ld",currentYear-100]];
        currentYear++;
    }
    currentYear = [yearArray[10] integerValue];
    if (self.dateMode == LJDatePickerViewModeMinAndSec) {
        [_pickerView selectRow:0 inComponent:0 animated:YES];
        selectOneRow = 0;
        [_pickerView selectRow:0 inComponent:2 animated:YES];
    }else if(self.dateMode == LJDatePickerViewModeDate){
        [_pickerView selectRow:10 inComponent:0 animated:YES];
        selectOneRow = 10;
        [_pickerView selectRow:currentMonth-1 inComponent:1 animated:YES];
        selectTwoRow = currentMonth-1;
        [_pickerView selectRow:currentDay-1 inComponent:2 animated:YES];
        selectThreeRow = currentDay-1;
    }else if (self.dateMode == LJDatePickerViewModeDateMinSec){
        [_pickerView selectRow:10 inComponent:0 animated:YES];
        selectOneRow = 10;
        [_pickerView selectRow:currentMonth-1 inComponent:1 animated:YES];
        selectTwoRow = currentMonth-1;
        [_pickerView selectRow:currentDay-1 inComponent:2 animated:YES];
        selectThreeRow = currentDay-1;
        [_pickerView selectRow:currentHour inComponent:3 animated:YES];
        selectFourRow = currentHour;
        [_pickerView selectRow:currentMintue inComponent:4 animated:YES];
        selectFiveRow = currentMintue;
    }
}

-(void)refreshDayArrayWithYear:(NSInteger)year  WithMonth:(NSInteger)month{
    NSInteger count = [TimeModel getDayCount:[NSString stringWithFormat:@"%ld-%ld",(long)year,month]];
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:@"01",@"02", @"03", @"04", @"05", @"06", @"07", @"08", @"09", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31",  nil];
    for (int i =0; i<31-count; i++) {
        [tempArray removeLastObject];
    }
    dayArray = [tempArray copy];
}

-(void)setCurrentDate:(NSString *)currentDate {
    _currentDate = currentDate;
    NSArray *array = [currentDate componentsSeparatedByString:@"-"];
    NSString *year = array.firstObject;
    NSString *month = array[1];
    NSString *day = array.lastObject;
    for (NSString *string in yearArray) {
        if ([year isEqualToString:string]) {
            NSInteger idx = [yearArray indexOfObject:string];
            [_pickerView selectRow:idx inComponent:0 animated:NO];
            [self pickerView:_pickerView didSelectRow:idx inComponent:0];
        }
    }
    for (NSString *string in monthArray) {
        if ([month isEqualToString:string]) {
            NSInteger idx = [monthArray indexOfObject:string];
            [_pickerView selectRow:idx inComponent:1 animated:NO];
            [self pickerView:_pickerView didSelectRow:idx inComponent:1];
        }
    }
    for (NSString *string in dayArray) {
        if ([day isEqualToString:string]) {
            NSInteger idx = [dayArray indexOfObject:string];
            [_pickerView selectRow:idx inComponent:2 animated:NO];
            [self pickerView:_pickerView didSelectRow:idx inComponent:2];
        }
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    switch (self.dateMode) {
        case 0:
            return 4;
            break;
        case 1:
            return 3;
            break;
        case 2:
            return 5;
            break;
        default:
            break;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.dateMode == 0) {
        if (component == 0) {
            return hourArray.count;
        }else if (component == 1){
            return 1;
        }else if (component == 2){
            return minuteArray.count;
        }else{
            return 1;
        }
    }else if(self.dateMode == 1){
        if (component == 0) {
            return yearArray.count;
        }else if (component == 1){
            return monthArray.count;
        }else{
            return dayArray.count;
        }
    }else {
        if (component == 0) {
             return yearArray.count;
        }else if(component == 1){
            return monthArray.count;
        }else if(component == 2){
            return dayArray.count;
        }else if(component == 3){
             return hourArray.count;
        }else{
            return minuteArray.count;
        }
    }
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        if (self.dateMode == 0) {
            return hourArray[row];
        }
        return yearArray[row];
    }else if (component == 1){
        if (self.dateMode == 0) {
            return @"时";
        }
        return monthArray[row];
    }else if (component == 2){
        if (self.dateMode == 1 || self.dateMode == 2) {
            return dayArray[row];
        }
        return minuteArray[row];
    }else if (component == 3){
        if (self.dateMode == 2) {
            return hourArray[row];
        }else{
         return @"分";
        }
    }else{
      return minuteArray[row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
         selectOneRow = row;
        if (self.dateMode == 0) {
            hourStr = hourArray[row];
        }else if(self.dateMode == 1 ||self.dateMode == 2){
            currentYear = [yearArray[row] integerValue];
        }
    }else if (component == 1) {
        if (self.dateMode == 1 || self.dateMode == 2) {
            currentMonth = [monthArray[row] integerValue];
            NSInteger indexMonth = [monthArray[row] integerValue];
            selectTwoRow = indexMonth - 1;
            [self refreshDayArrayWithYear:currentYear WithMonth:currentMonth];
        }
    }else if (component == 2) {
        if (self.dateMode == 1 || self.dateMode == 2) {
            currentDay = [dayArray[row] integerValue];
            NSInteger indexDay = [dayArray[row] integerValue];
            selectThreeRow = indexDay - 1;
        }else{
            minuteStr = minuteArray[row];
            selectThreeRow =row;
        }
    }else if (component == 3) {
        if (self.dateMode == 2) {
            currentHour = [hourArray[row] integerValue];
            NSInteger indexDay = [hourArray[row] integerValue];
            selectFourRow = indexDay;
        }
    }else{
        if (self.dateMode == 2) {
            currentMintue = [minuteArray[row] integerValue];
            NSInteger indexDay = [minuteArray[row] integerValue];
            selectFiveRow = indexDay;
        }
    }
    [_pickerView reloadAllComponents];
}

-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置pickView的线条的颜色
    for (UIView* designView in pickerView.subviews) {
        if (designView.frame.size.height < 1) {
            designView.backgroundColor = HexColor(0x999999);
            designView.alpha =0.3;
        }
    }
    UILabel* label = (UILabel*)view;
    if (!label) {
        label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:17.0];
        label.textAlignment = NSTextAlignmentCenter;
     
        if (component == 0 && selectOneRow == row) {
            label.textColor = Color_ThemeColor;
        }else if(component == 1 && selectTwoRow==row){
            if (self.dateMode == 1 || self.dateMode == 2) {
                label.textColor = Color_ThemeColor;
            }else{
                label.textColor = HexColor(0x999999);
            }
        }else if(component == 2 && selectThreeRow == row){
            label.textColor = Color_ThemeColor;
        }else if (component == 3 && selectFourRow == row){
            if (self.dateMode == 2) {
                label.textColor = Color_ThemeColor;
            }else{
                label.textColor = HexColor(0x999999);
            }
        }else if (component == 4 && selectFiveRow == row){
            if (self.dateMode == 2) {
                label.textColor = Color_ThemeColor;
            }else{
                label.textColor = HexColor(0x999999);
            }
        }else{
            if (self.dateMode == 0) {
                label.textColor=HexColor(0x999999);
            }
        }
    }
    label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return label;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (self.dateMode == 0) {
        if (component == 1) {
            return 20;
        }else if (component == 3) {
            return 20;
        }return ScreenW/4;
    }else if (self.dateMode == 2){
        return (ScreenW-40)/5;
    }
    return ScreenW/4;
}

- (void)showWithDate:(LJDatePickerFinishBlock)finishBlock {
    UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
    [keyWin addSubview:self];
    [UIView animateWithDuration:0.2 animations:^{
        self.baseView.nim_top =ScreenH-250-BottomHeight;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.baseView.nim_top = ScreenH-250-BottomHeight;
        } completion:^(BOOL finished) {
            self.finishBlock = finishBlock;
        }];
    }];
}

//点击完成按钮的传值
-(void)finishClick{
    if (self.dateMode == 0) {
        if (hourStr == nil && minuteStr == nil) {
            self.finishBlock([NSString stringWithFormat:@"%@:%@",hourArray[0],minuteArray[0]]);
        }else if (hourStr == nil && minuteStr != nil){
            self.finishBlock([NSString stringWithFormat:@"%@:%@",hourArray[0],minuteStr]);
        }else if (hourStr != nil && minuteStr == nil){
            self.finishBlock([NSString stringWithFormat:@"%@:%@",hourStr,minuteArray[0]]);
        }else{
            self.finishBlock([NSString stringWithFormat:@"%@:%@",hourStr,minuteStr]);
        }
    }else if (self.dateMode == 1){
        self.finishBlock([NSString stringWithFormat:@"%ld-%@-%@",(long)currentYear,[TimeModel strMonthFromIntMonth:currentMonth],[TimeModel strDayFromIntDay:currentDay]]);
    }else if (self.dateMode == 2){
        self.finishBlock([NSString stringWithFormat:@"%ld-%@-%@ %@:%@",(long)currentYear,[TimeModel strMonthFromIntMonth:currentMonth],[TimeModel strDayFromIntDay:currentDay],currentHour==0?[NSString stringWithFormat:@"00"]: currentHour<10 ?[NSString stringWithFormat:@"0%ld",(long)currentHour] : [NSString stringWithFormat:@"%ld",(long)currentHour],currentMintue==0?[NSString stringWithFormat:@"00"]: currentMintue<10 ? [NSString stringWithFormat:@"0%ld",(long)currentMintue] : [NSString stringWithFormat:@"%ld",(long)currentMintue]]);
    }
    [self hide];
}
//取消按钮的点击事件
-(void)hide{
    [UIView animateWithDuration:0.2 animations:^{
        self.baseView.nim_top = ScreenH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hide];
}

@end
